package com.iot.inclinometer.util;



import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Map;


/**
 * Http请求的工具类
 *
 * @author
 *
 */
public class XUtilsHttp
{

    /**
     * 发送get请求
     * @param <T>
     */
    public static <T> Callback.Cancelable Get(String url,Map<String,Object> map,Callback.CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);

        if(null!=map){
            for(Map.Entry<String, Object> entry : map.entrySet()){
                params.addQueryStringParameter(entry.getKey(), (String)entry.getValue());
            }
        }
//        if(YiwugouApplication.isShowLog){
//            Logger.getLogger(XUtilsHttp.class).d("get url=%s",params.toString());
//        }

        Callback.Cancelable cancelable = x.http().get(params, callback);

        return cancelable;
    }

    /**
     * 发送post请求
     * @param <T>
     */
    public static <T> Callback.Cancelable Post(String url,Map<String,Object> map,Callback.CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        if(null!=map){
            for(Map.Entry<String, Object> entry : map.entrySet()){
                params.addParameter(entry.getKey(), entry.getValue());
            }
        }
//        if(YiwugouApplication.isShowLog){
//            Logger.getLogger(XUtilsHttp.class).d("post url=%s",params.toString());
//        }
        Callback.Cancelable cancelable = x.http().post(params, callback);

        return cancelable;
    }


    /**
     * 上传文件
     * @param <T>
     */
    public static <T> Callback.Cancelable UpLoadFile(String url,Map<String,Object> map,Callback.CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        if(null!=map){
            for(Map.Entry<String, Object> entry : map.entrySet()){
                params.addParameter(entry.getKey(), entry.getValue());
            }
        }
        params.setMultipart(true);
        Callback.Cancelable cancelable = x.http().post(params, callback);
        return cancelable;
    }

    /**
     * 下载文件
     * @param <T>
     */
    public static <T> Callback.Cancelable DownLoadFile(String url,String filepath,Callback.CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        //设置断点续传
        params.setAutoResume(true);
        params.setSaveFilePath(filepath);
        Callback.Cancelable cancelable = x.http().get(params, callback);
        return cancelable;
    }




    public  static DbManager initDB(String name){
        DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
                .setDbName(name)
                        // 不设置dbDir时, 默认存储在app的私有目录.
                        //.setDbDir(new File("/sdcard")) // "sdcard"的写法并非最佳实践, 这里为了简单, 先这样写了.
                .setDbVersion(2)
                .setDbOpenListener(new DbManager.DbOpenListener() {
                    @Override
                    public void onDbOpened(DbManager db) {
                        // 开启WAL, 对写入加速提升巨大
                        db.getDatabase().enableWriteAheadLogging();
                    }
                })
                .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                    @Override
                    public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                        // TODO: ...
                        // db.addColumn(...);
                        // db.dropTable(...);
                        // ...
                        // or
                        // db.dropDb();
                    }
                });
        return    x.getDb(daoConfig);
    }

}